package io.renren.modules.generator.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.generator.entity.UserFriendEntity;
import io.renren.modules.generator.service.UserFriendService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * @author jianzhen
 * @email sunlightcs@gmail.com
 * @date 2022-04-10 11:24:32
 */
@RestController
@RequestMapping("generator/userfriend")
@Transactional
public class UserFriendController {
    @Autowired
    private UserFriendService userFriendService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = userFriendService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        UserFriendEntity userFriend = userFriendService.getById(id);

        return R.ok().put("userFriend", userFriend);
    }



    @RequestMapping("/deleteFirend/{id}")
    public R deleteFirend(@PathVariable("id") Long friendId) {
        userFriendService.deleteFirend(friendId);

        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UserFriendEntity userFriend) {
        userFriendService.save(userFriend);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserFriendEntity userFriend) {
        userFriendService.updateById(userFriend);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        userFriendService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 我的好友列表
     */
    @RequestMapping("/friendList")
    public R friendList(@RequestParam Map<String, Object> params) {
        params.put("userId", ShiroUtils.getUserId());
        Page<Map<String, Object>> page = new Page<>(Long.valueOf(String.valueOf(params.get("page"))), Long.valueOf(String.valueOf(params.get("limit"))));
        IPage<Map<String, Object>> mapIPage = userFriendService.selectFriendList(page, params);
        PageUtils newPage = new PageUtils(mapIPage);
        return R.ok().put("page", newPage);
    }


    /**
     * 添加好友列表
     */
    @RequestMapping("/addFriendList")
    public R addFriendList(@RequestParam Map<String, Object> params) {
        PageUtils pageUtils = userFriendService.selectAddFriendList(params);
        return R.ok().put("page", pageUtils);
    }

}
